using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/****************************************************
// 功能：小地图控制
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2025/3/27 15:3:8
*****************************************************/

public partial class PlayerControlWindow
{
    public Transform mapIconRoot;
    public Transform mapHeroIconRoot;
    
    private Dictionary<MainLogicUnit, ItemMapIcon> itemDic;
    // 场景中心位置默认为（0，0，0）
    private readonly Vector3 refPos = Vector3.zero;
    
    
    public void InitMiniMap() {
        itemDic = new Dictionary<MainLogicUnit, ItemMapIcon>();
    }

    void UnInitMiniMap() {
        for(var i = mapHeroIconRoot.childCount - 1; i >= 0; --i) {
            Destroy(mapHeroIconRoot.GetChild(i).gameObject);
        }
        for(var i = mapIconRoot.childCount - 1; i >= 0; --i) {
            if(mapIconRoot.GetChild(i).name != "bgRoad") {
                Destroy(mapIconRoot.GetChild(i).gameObject);
            }
        }

        itemDic?.Clear();
    }

    public void AddMiniIconItemInfo(MainLogicUnit unit) {
        if(itemDic.ContainsKey(unit)) {
            Debug.LogError(unit.unitName + "minimap item is already exist.");
            return;
        }
        if(gameObject.activeSelf == false) {
            return;
        }
        var path = GetItemPath(unit.unitType);
        var go = resource.LoadPrefab(path, null, true);
        go.transform.SetParent(unit.unitType == UnitTypeEnum.Hero ? mapHeroIconRoot : mapIconRoot);
        go.transform.localPosition = Vector3.zero;
        go.transform.localScale = Vector3.one;

        var ih = go.GetComponent<ItemMapIcon>();
        ih.InitItem(unit, refPos);
        itemDic.Add(unit, ih);
    }

    string GetItemPath(UnitTypeEnum unitType) {
        string path = "";
        switch(unitType) {
            case UnitTypeEnum.Hero:
                path = "Prefab/DynamicItem/ItemMapIconHero";
                break;
            case UnitTypeEnum.Soldier:
            case UnitTypeEnum.Tower:
                path = "Prefab/DynamicItem/ItemMapIcon";
                break;
            default:
                break;
        }
        return path;
    }

    public void RemoveMapIconItemInfo(MainLogicUnit key)
    {
        if (!itemDic.TryGetValue(key, out var item)) return;
        Destroy(item.gameObject);
        itemDic.Remove(key);
    }
}
